1   /*
2    * Copyright (c) 2004-2005, University Health Network.  All rights reserved. Distributed under the BSD 
3    * license (see http://opensource.org/licenses/bsd-license.php).
4    *  
5    * Created on 24-Jan-2005
6    */
7   package ca.uhn.cache.util;
8   
9   import ca.uhn.cache.IDimension;
10  import ca.uhn.cache.IParamSpace;
11  import ca.uhn.cache.IQuery;
12  import ca.uhn.cache.IQueryParam;
13  import ca.uhn.cache.impl.Dimension;
14  import ca.uhn.cache.impl.ParamSpace;
15  import ca.uhn.cache.impl.Query;
16  import ca.uhn.cache.impl.StringParam;
17  import ca.uhn.cache.impl.StringSetParam;
18  import ca.uhn.cache.internal.IParamSpaceConfig;
19  import ca.uhn.cache.internal.impl.ParamSpaceConfig;
20  import junit.framework.TestCase;
21  
22  /***
23   * Test cases for QueryUtil. 
24   * 
25   * @author <a href="mailto:bryan.tripp@uhn.on.ca">Bryan Tripp</a>
26   * @version $Revision: 1.2 $ updated on $Date: 2005/01/25 22:04:21 $ by $Author: aguevara $
27   */
28  public class QueryUtilTest extends TestCase {
29  
30      private IParamSpace myParamSpace;
31      private IDimension myDimOne;
32      private IDimension myDimTwo;
33      private IQueryParam myBoundary11;
34      private IQueryParam myBoundary12;
35      private IQueryParam myBoundary21;
36      private IQueryParam myBoundary22;
37      
38      /***
39       * Constructor for QueryUtilTest.
40       * @param theName ...
41       */
42      public QueryUtilTest(String theName) {
43          super(theName);
44      }
45  
46      /***
47       */
48      public void testChunk() {
49          IQuery query = new Query();
50          query.addParameter(new StringSetParam(myDimOne, new String[]{"a", "b", "c", "d"}));
51          query.addParameter(new StringSetParam(myDimTwo, new String[]{"w", "x", "y", "z"}));
52          
53          IQuery[] regions = QueryUtil.chunk(query, myParamSpace);
54  
55          assertEquals(4, regions.length);
56          
57          assertTrue(isThere(regions, myBoundary11, myBoundary21));
58          assertTrue(isThere(regions, myBoundary11, myBoundary22));
59          assertTrue(isThere(regions, myBoundary12, myBoundary21));
60          assertTrue(isThere(regions, myBoundary12, myBoundary22));
61      }
62      
63      private boolean isThere(IQuery[] theRegions, IQueryParam theOneBoundary, IQueryParam theTwoBoundary) {
64          boolean there = false;
65          
66          for (int i = 0; i < theRegions.length && !there; i++) {
67              if (matches(theRegions[i], new IQueryParam[]{theOneBoundary, theTwoBoundary})) {
68                  there = true;
69              }
70          }
71          
72          return there;
73      }
74      
75      private boolean matches(IQuery theRegion, IQueryParam[] theBoundaries) {
76          boolean matches = true;
77          
78          for (int i = 0; i < theBoundaries.length && matches; i++) {
79              if (!theBoundaries[i].equals(theRegion.getParamByDimension(theBoundaries[i].getDimension()))) {
80                  matches = false;
81              }
82          }
83  
84          return matches;
85      }
86  
87      protected void setUp() throws Exception {
88          myDimOne = new Dimension("one", new Class[] {StringParam.class, StringSetParam.class});
89          myDimTwo = new Dimension("two", new Class[] {StringParam.class, StringSetParam.class});
90          
91          //chunk boundaries
92          myBoundary11 = new StringSetParam(myDimOne, new String[]{"a", "b"});
93          myBoundary12 = new StringSetParam(myDimOne, new String[]{"c", "d"});
94          myBoundary21 = new StringSetParam(myDimTwo, new String[]{"w", "x"});
95          myBoundary22 = new StringSetParam(myDimTwo, new String[]{"y", "z"});
96          
97          IParamSpaceConfig config = new ParamSpaceConfig(new IDimension[]{myDimOne, myDimTwo}, 
98                  new IQueryParam[] {myBoundary11, myBoundary12, myBoundary21, myBoundary22}, new IQueryParam[0]);
99                  
100         myParamSpace = ParamSpace.createInstance(config);
101     }
102 }